package com.empire.module.member.service.accounttype;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.empire.framework.common.pojo.PageResult;
import com.empire.module.member.dal.dataobject.accounttype.AccountTypeDO;
import com.empire.module.member.dal.dataobject.internetaccount.InternetAccountDO;
import com.empire.module.member.dal.mysql.accounttype.AccountTypeMapper;
import com.empire.module.member.dal.mysql.internetaccount.InternetAccountMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import com.empire.module.member.controller.admin.accounttype.vo.*;
import com.empire.framework.common.util.object.BeanUtils;

import javax.annotation.Resource;

import java.util.List;

import static com.empire.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.empire.module.member.enums.ErrorCodeConstants.*;

/**
 * 账号平台 Service 实现类
 *
 * @author 主席大人
 */
@Service
@Validated
public class AccountTypeServiceImpl implements AccountTypeService {

    @Resource
    private AccountTypeMapper accountTypeMapper;

    @Resource
    private InternetAccountMapper internetAccountMapper;

    @Override
    public Long createAccountType(AccountTypeSaveReqVO createReqVO) {
        // 插入
        AccountTypeDO accountType = BeanUtils.toBean(createReqVO, AccountTypeDO.class);
        accountTypeMapper.insert(accountType);
        // 返回
        return accountType.getId();
    }

    @Override
    public void updateAccountType(AccountTypeSaveReqVO updateReqVO) {
        // 校验存在
        validateAccountTypeExists(updateReqVO.getId());
        // 更新
        AccountTypeDO updateObj = BeanUtils.toBean(updateReqVO, AccountTypeDO.class);
//        accountTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteAccountType(Long id) {
        // 校验存在
        validateAccountTypeExists(id);
        // 先查询是否已经存在账号引用，如果有，不允许删除
        notAllowAccountTypeExists(id);
        // 删除
        accountTypeMapper.deleteById(id);
    }

    private void validateAccountTypeExists(Long id) {
        if (accountTypeMapper.selectById(id) == null) {
            throw exception(ACCOUNT_TYPE_NOT_EXISTS);
        }
    }

    private void notAllowAccountTypeExists(Long id) {
        LambdaQueryWrapper<InternetAccountDO> query = new LambdaQueryWrapper<>();
        query.eq(InternetAccountDO::getAccountTypeId, id);
        query.last("limit 1");
        InternetAccountDO internetAccountDO = internetAccountMapper.selectOne(query);
        if (internetAccountDO != null) {
            throw exception(ACCOUNT_TYPE_EXISTS_DATA);
        }
    }

    @Override
    public AccountTypeDO getAccountType(Long id) {
        return accountTypeMapper.selectById(id);
    }

    @Override
    public PageResult<AccountTypeDO> getAccountTypePage(AccountTypePageReqVO pageReqVO) {
        return accountTypeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AccountTypeDO> getAccountTypeList() {
        return accountTypeMapper.selectList();
    }

}